<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>匿名函数 箭头函数</title>
</head>
<body>
<script>
    let array = ["张三", "李四", "王五", "赵六"]

    var name = "abcque"

    var hobby  = "flower"

    // 箭头函数
    array.forEach(item =>{
        console.log(item);
        console.log(name);
        console.log(this.hobby);
        console.log(window.hobby);
    })

    array.forEach((item) =>{
        console.log(item);
        console.log(name);
        console.log(this.hobby);
        console.log(window.hobby);
    })

    // 匿名函数
    array.forEach(function (item) {
        console.log(item);
        console.log(name);
        console.log(this.hobby);
        console.log(window.hobby);
    })

    const obj1 = {
        name: 'John',
        greet: function() {
            setTimeout(() => {
                console.log('Hello, ' + this.name);
            }, 100);
        }
    };
    obj1.greet(); // 输出: Hello, John

    const obj2 = {
        name: 'John',
        greet: function() {
            setTimeout(function() {
                console.log('Hello, ' + this.name);
            }, 100);
        }
    };
    obj2.greet(); // 输出: Hello, undefined
</script>
</body>
</html>